package com.darly.std.queue.masterwoker;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Description TODO: 处理工人
 * Package com.darly.std.queue.masterwoker
 * Author fronchdarly
 * Date 2021/8/4 10:50 AM
 * Where: 陕西西安
 * How:
 */
public abstract class Worker implements Runnable {

    //1:应该有一个承载任务的集合
    private ConcurrentLinkedQueue<Mession> workQueue;
    //2:使用一个容器承载每一个worker并行执行任务的结果集
    private ConcurrentHashMap<String, Object> resultMap;

    public void setWorkQueue(ConcurrentLinkedQueue<Mession> workQueue) {
        this.workQueue = workQueue;
    }


    public void setResultMap(ConcurrentHashMap<String, Object> resultMap) {
        this.resultMap = resultMap;
    }


    @Override
    public void run() {
        while (true) {
            Mession mession = this.workQueue.poll();
            if (mession == null) {
                break;
            }
            Object out = startHandle(mession);
            this.resultMap.put(String.valueOf(mession.getId()), out);
        }
    }

    public abstract Object startHandle(Mession mession);

}
